package com.ys.zero.web;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ys.zero.bean.Comment;
import com.ys.zero.bean.Result;
import com.ys.zero.bean.User;
import com.ys.zero.biz.BizException;
import com.ys.zero.biz.CommentBiz;
import com.ys.zero.dao.CommentDao;

@WebServlet("/comment/*")
public class CommentServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
    
	private CommentBiz biz = new CommentBiz();
	private CommentDao dao = new CommentDao();
	/**
	 * 插入
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
    protected void insertCommment(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
    	String content = request.getParameter("content");
    	System.out.println("content: "+content);
		String cuaid = request.getParameter("cuaid");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("loginedUser");
		/* System.out.println(user.getAccount()); */
		try {
			biz.insertCommment(cuaid,user.getAccount(),content);
			toJson(response,user);
		} catch (BizException e) {
			toJson(response,new Result(0,e.getMessage()));
		}
	}
    
//    protected void hcomment(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException{
//    	String bhaccount = request.getParameter("bhaccount");
//		String huaid = request.getParameter("huaid");
//		String hcomment = request.getParameter("hcomment");
//		HttpSession session = request.getSession();
//		User user = (User) session.getAttribute("loginedUser");
//		/* System.out.println(user.getAccount()); */
//		try {
//			biz.hcomment(huaid,bhaccount,user.getAccount(),hcomment);
//			toJson(response,user);
//		} catch (BizException e) {
//			toJson(response,new Result(0,e.getMessage()));
//		}
//	}
    
    
    protected void getUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
    	HttpSession session = request.getSession();
		User user = (User) session.getAttribute("loginedUser");
		toJson(response,user);
    }
    
    
//    protected void delCommment(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException{
//    	String cuaid = request.getParameter("cuaid");
//    	String coaccount = request.getParameter("coaccount");
//    	String comment = request.getParameter("comment");
//    	dao.del(cuaid,coaccount,comment);
//    }
//    
    protected void dzCommment(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
    	String cuaid = request.getParameter("cuaid");
    	String coaccount = request.getParameter("coaccount");
    	String comment = request.getParameter("comment");
    	dao.dz(cuaid,coaccount,comment);
    }
    
    protected void qxdzCommment(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
    	String cuaid = request.getParameter("cuaid");
    	String coaccount = request.getParameter("coaccount");
    	String comment = request.getParameter("comment");
    	dao.qxdz(cuaid,coaccount,comment);
    }
    
    /**
     * 查询一篇文章相关评论数据
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void queryComment(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
    	String cuaid = request.getParameter("cuaid");
    	try {
    		List<Map<String, Object>> comments = biz.queryComment(cuaid);
    		toJson(response, comments);
		} catch (BizException e) {
			toJson(response,new Result(0,e.getMessage()));
		}
    	
    }
    
    protected void queryMyComment(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
    	User user = (User) request.getSession().getAttribute("loginedUser");
    	
		List<Comment> comments = dao.queryMyComment(user.getAccount());
		toJson(response, comments);
    }
    
    protected void delMyComment(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
    	String cid = request.getParameter("cid");
    	
		dao.delMyComment(cid);
    }

}
